package com.ruoyi.quartz.task;

import com.ruoyi.control.mapper.BeltHistoryMapper;
import com.ruoyi.control.mapper.CentralpumphouseHistoryMapper;
import com.ruoyi.control.mapper.ElectricityHistoryMapper;
import com.ruoyi.control.service.impl.*;
import com.ruoyi.situation.service.impl.SituationServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private ElectricityHistoryServiceImpl electricityHistoryService;

    @Autowired
    private BeltHistoryServiceImpl beltHistoryService;

    @Autowired
    private CentralpumphouseHistoryServiceImpl centralpumphouseHistoryService;

    @Autowired
    private Mainventilator1HistoryServiceImpl mainventilator1HistoryService;

    @Autowired
    private Mainventilator2HistoryServiceImpl mainventilator2HistoryService;

    @Autowired
    private SituationServiceImpl situationService;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params) {
        System.out.println("执行有参方法：" + params);
    }

//    public void ryNoParams()
//    {
//        System.out.println("执行无参方法");
//    }

    /**
     * 每日创建 safety_point_history
     */
    public void ryNoParams() {

        System.out.println("执行无参方法");



        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String tableName = "safety_point_history_" + dateFormat.format(new Date());

        String createTableSQL = "CREATE TABLE IF NOT EXISTS " + tableName + " (" +
                "id VARCHAR(255) NOT NULL, " +
                "point_code VARCHAR(50) NOT NULL, " +
                "sensor_type_name VARCHAR(50), " +
                "point_install_location VARCHAR(100), " +
                "point_value VARCHAR(50), " +
                "point_value_unit VARCHAR(10), " +
                "point_status VARCHAR(50), " +
                "upload_time DATETIME, " +
                "extend1 VARCHAR(255), " +
                "extend2 VARCHAR(255), " +
                "PRIMARY KEY (id)" +
                ") CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;";


        try (Connection connection = dataSource.getConnection();
             Statement statement = connection.createStatement()) {
            statement.execute(createTableSQL);
            System.out.println("历史表 " + tableName + " 创建成功！");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("创建历史表时发生错误！");
        }


    }


    /**
     * 生产态势分析得分    安全态势得分  定时计算
     */
    public void productScore() {
        electricityHistoryService.getElectricTotal();
        beltHistoryService.getBeltTotal();
        centralpumphouseHistoryService.getWaterTotal();
        mainventilator1HistoryService.getMainOneTotal();
        mainventilator2HistoryService.getMainTwoTotal();

        //安全
        situationService.getScoreList();
    }

}
